// 引入封装好的请求
import { getKnowledge, delKnowledge, newKnowledge, editorKnowledge, showKnowledge } from '@/api/knowledge'

const state = {
  KnowledgeList: [],
  pageCount: 0, // 数字类型
  editorKnow: {}
}

const mutations = {
  update(state, payload) {
    for (const key in payload) {
      state[key] = payload[key]
    }
  }
}

const actions = {
  // 请求所有的数据
  async getKnow({ commit }, payload = {}) {
    // console.log(payload);
    const { page = 1, pageSize = 8, ...options } = payload
    console.log(page, pageSize)
    const result = await getKnowledge(page, pageSize, options)
    // console.log(result)
    if (result.data) {
      commit('update', {
        KnowledgeList: result.data[0],
        pageCount: result.data[1]
      })
    }
    // console.log(this.state.knowledge.KnowledgeList);
  },

  // 删除数据
  async delKnowledge({ commit }, payload) {
    // console.log(payload)
    const result = await delKnowledge(payload)
    console.log(result)
    // if (result.statusCode == 200) {
    //   // 删除成功后刷新页面
    //   window.location.reload()
    // }
  },

  // 新增数据
  async newKnowledge({ commit }, payload) {
    // console.log(payload)
    const result = await newKnowledge(payload)
    console.log(result)
  },

  // 编辑 跳一个详情
  async editorKnowledge({ commit }, payload) {
    // console.log(payload)
    const result = await editorKnowledge(payload)
    // console.log(result.data)
    if (result.data) {
      commit('update', {
        editorKnow: result.data
      })
    }
    // console.log(this.state.knowledge.editorKnow);
  },

  // 回显完，修改页面
  async showKnowledge({ commit }, payload) {
    // console.log(payload);
    const result = await showKnowledge(payload)
    // console.log(result);
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
